package com.menggudf;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;

import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class HtmlToPdfConverter1 {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://wuliu-hangle.oss-cn-guangzhou.aliyuncs.com/20231202/ukdpd/1086340299.html");
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
            StringBuilder htmlContent = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                htmlContent.append(line);
            }
            reader.close();

            PDDocument document = new PDDocument();
            PDPage page = new PDPage(PDRectangle.A4);
            document.addPage(page);

            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            contentStream.beginText();
         //   contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
            contentStream.newLineAtOffset(25, 700);
            contentStream.showText(htmlContent.toString());
            contentStream.endText();
            contentStream.close();

            document.save("output.pdf");
            document.close();
            System.out.println("PDF文件已生成: output.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}